﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Com.CMS263.PO;
namespace Com.CMS263.DAO
{
    public  class CategoryDAO:BaseService
    {

        public IList<Category> FindAll()
        {
            string hql = "from Category order by SortOrder desc";
            return FindList<Category>(hql, null, null, null);
        }

        public int FindListByKey(string key)
        {
            try
            {
                string hql = "from Category";
                if (!string.IsNullOrEmpty(key))
                {
                    hql += " where (CatName like '%" + key + "%' or KeyWord like '%" + key + "%' )";
                }
                return DBHelper.ExecuteHQL(hql + "  order by SortOrder desc").List<Category>().Count;
            }
            catch 
            {

                return 0;
            }
        }

        public IList<Category> FindListByKey(string key, int pagesize, int pageindex)
        {
            try
            {
                string hql = "from Category";
                if (!string.IsNullOrEmpty(key))
                {
                    hql += " where ( CatName like '%" + key + "%' or KeyWord like '%" + key + "%' )";
                }
                return DBHelper.ExecuteHQL(hql + "  order by SortOrder desc").SetFirstResult((pageindex - 1) * pagesize).SetMaxResults(pagesize).List<Category>();
            }
            catch { return null; }
        }
        /// <summary>
        /// 查询父节点
        /// </summary>
        /// <returns></returns>
        public IList<Category> FindListPro()
        {
            string hql = "from Category where ParentId=0 order by SortOrder desc";
            return FindList<Category>(hql, null, null, null);
        }


        /// <summary>
        /// 添加禁止重复
        /// </summary>
        /// <param name="parentId"></param>
        /// <param name="caName"></param>
        /// <returns></returns>
        public Category FindModelByName(int parentId, string caName)
        {
            string hql = "from Category where CatName='"+caName+"' ";
            if (parentId==0)
            {
                hql += " and  ParentId=0";
            }
            else
            {
                hql += " and  ParentId>0";
            }
            try
            {
                return FindList<Category>(hql, null, null, null)[0];
            }
            catch 
            {
                
                return null;
            }
        }

        public IList<Category> FindByParentId(int parentId)
        {
            string hql = "from Category where ParentId=" + parentId + " order by SortOrder desc";
            return FindList<Category>(hql, null, null, null);
        }
    }
}
